//
//  ReviewViewController.m
//  OrderSeat
//
//  Created by haiming on 13-10-7.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "ReviewViewController.h"
#import "ShopHomeViewController.h"
#import "ReviewCell.h"

@implementation ReviewViewController

- (id)init
{
    self = [super initWithNibClass:[ReviewViewController class]];
    if (self) {
        _dataSource = [[ReviewDataSource alloc] initWithVC:self];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
    [self autoPullRefresh];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[AppDelegate sharedInstance] hideTabBar:self.tabBarController];
}

- (void)setupUI
{
    [self createLeftButton];
    [self createNavigation:@"我的点评"];
    [self createHeaderViewForTable:_reviewTable];
    
    _reviewTable.dataSource = _dataSource;
    _reviewTable.delegate = _dataSource;
}

- (void)leftButtonClick:(id)sender
{
    [[AppDelegate sharedInstance] showTabBar:self.tabBarController];
    [self.navigationController popViewControllerAnimated:YES];
}

//刷新调用的方法
-(void)refreshView
{
    _dataSource.pageNumber = 1;
    [_dataSource fetchReview];
}

- (void)finishedLoadData
{
    [super finishedLoadData];
    [self addEmptyText];
}

- (void)addEmptyText
{
    if (_dataSource.reviewList.count == 0) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, self.view.width, 30)];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:20];
        label.textColor = [UIColor grayColor];
        label.text = @"没有数据显示";
        label.tag = 1234;
        [self.view addSubview:label];
    }else {
        UIView *view = [self.view viewWithTag:1234];
        [view removeFromSuperview];
    }
}

#pragma 将分页加载的数据添加到tableview后面
- (void)appendTableWith:(NSMutableArray *)data
{
    for (int i = 0; i < [data count]; i++) {
        [_dataSource.reviewList addObject:[data objectAtIndex:i]];
    }
    
    NSMutableArray *insertIndexPaths = [NSMutableArray arrayWithCapacity:10];
    
    for (int ind = 0; ind < [data count]; ind++) {
        NSIndexPath *newPath = [NSIndexPath indexPathForRow:[_dataSource.reviewList indexOfObject:[data objectAtIndex:ind]] inSection:0];
        [insertIndexPaths addObject:newPath];
    }
    
    [_reviewTable insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
    
    self.moreLoading = NO;
}

@end

//======================================================================================
#pragma ===================数据源类实现===================================================
//======================================================================================

#define RefreshTag 1001
#define LoadMoreTag 1002
#define RemoveTag 1003

@implementation ReviewDataSource

- (id)initWithVC:(id)viewController
{
    self = [super initWithVC:viewController];
    if(self) {
        self.viewController = viewController;
        _reviewList = [[NSMutableArray alloc] initWithCapacity:5];
    }
    return self;
}

- (void)fetchReview
{
    NSString *url = [NSString stringWithFormat:API_REVIEW, [AppSetting userName], _pageNumber];
    [self get:url tag:_pageNumber == 1 ? RefreshTag : LoadMoreTag];
}

- (void)removeWithId:(NSString *)commId
{
    NSString *url = [NSString stringWithFormat:API_DEL_COMMENTS,commId];
    [self get:url tag:RemoveTag];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    CLog(@"%@", request.responseString);
    
    NSDictionary *json = [request.responseData JSONValue];
    NSDictionary *data = [json objectForKey:@"data"];
    NSMutableArray *list = [data objectForKey:@"pinglun"];
    
    if (request.tag == RefreshTag) {
        self.reviewList = list;
        [_viewController finishedLoadData];
    }
    
    if (request.tag == LoadMoreTag) {
        [_viewController appendTableWith:list];
    }
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    _pageNumber--;
    [_viewController finishedLoadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _reviewList.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellId = @"ReviewCell";
    ReviewCell *cell = [_viewController createCellWithNib:CellId tableview:tableView];
    
    NSDictionary *dic = [_reviewList objectAtIndex:indexPath.row];
    [cell setContent:dic];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [_reviewList objectAtIndex:indexPath.row];
    NSString *shopId = [dic objectForKey:@"did"];
    
    ShopHomeViewController *shopHomeVC = [[ShopHomeViewController alloc] initWithId:shopId];
    [AppDelegate changeToShopTabBar:shopHomeVC];
    
    [_viewController pushViewController:shopHomeVC];
}

#pragma 分页加载
-(void)tableView:(UITableView *)tableView  willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == _reviewList.count-1) {
        if (!_viewController.moreLoading) {
            _pageNumber++;
            [self fetchReview];
            _viewController.moreLoading = YES;
        }
    }
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        NSDictionary *dic = [_reviewList objectAtIndex:indexPath.row];
        NSString *_id = [dic objectForKey:@"id"];
        
        [_reviewList removeObject:dic];
        [self removeWithId:_id];
        
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];        
    }
}

@end
